﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using XingLucifer.Devices;
using XingLucifer.Devices.Models;
using XingLucifer.IBase;
using XingLucifer.IBase.Enums;

namespace TestDemo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("开始通信串口服务器");
            ICommunication com = null;
            var tokenSource = new System.Threading.CancellationTokenSource();
            com = new TCPCom(239, "192.168.10.100", 502, DeviceTypes.Modbus_TCP_BADC, 1, TaskTypes.扫描信号, "", tokenSource, EndianTypes.Modbus_BADC);
            var devices = EquipmentFactory.NewSocketDevice(239, "192.168.10.100", 502, XingLucifer.IBase.Enums.DeviceTypes.Modbus_TCP_BADC, 1, XingLucifer.IBase.Enums.TaskTypes.扫描信号, "测试", tokenSource, com);
            //if (devices.Open())
            //{
            //    Console.WriteLine("打开成功");
            //    //BatteryArrayModel batteryArrayModel = new BatteryArrayModel(1);
            //    Stopwatch stopwatch = new Stopwatch();
            //    stopwatch.Restart();
            //    BatteryArrayModel injectionDataArrayModel = new BatteryArrayModel(4);
            //    for (int i = 0; i < 1000; i++)
            //    {
            //        devices.ReadClass("PC_Miscellaneous.ShortCicute", injectionDataArrayModel);
            //       // Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(injectionDataArrayModel));
            //    }
            //    stopwatch.Stop();
            //    Console.WriteLine($"连续读取1000次 耗时==>>{stopwatch.ElapsedMilliseconds} ms");
            //    //Console.WriteLine("====================写入===================");
            //    ////devices.WriteSingle("1256069580", "PC_ScanCode.BeforeScan[1].Code");
            //    //devices.WriteSingle((long)12345678, "PC_ScanCode.BeforeScan[1].face.ID");
            //    //devices.WriteSingle((short)2, "PC_CMD.BeforeScanCMD[0]");

            //    //BatteryModel batteryModel = new BatteryModel() { ID = 1646464, Data_PLC = 63.24F, Data_PC = 23.36F, Results_PC = 666, Results_PLC = 45 };
            //    //devices.WriteClass(batteryModel, label:"PC_ScanCode.BeforeScan[0].face", crc:35167);
            //}
            //else
            //{
            //    Console.WriteLine("打开失败");
            //}
            if (devices.Open())
            {
                Console.WriteLine("打开成功!!!");
                //Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(devices.ReadClass<DUT_Battery>(24020)));
                Console.WriteLine(devices.ReadSingle<string>(24000, 17));
            }
            else
            {
                Console.WriteLine("打开通信失败！！！");
            }

            devices.Close();
            Console.WriteLine("关闭通信");
            Console.ReadLine();
        }

        //static void Main(string[] args)
        //{
            //Console.WriteLine("开始通信串口服务器");
            //CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            ////var com = new SerialPortCom(200, "COM2", 9600,
            ////            System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One, DeviceTypes.DTSU666, 1, TaskTypes.DTSU666, "测试", cancellationTokenSource, EndianTypes.Modbus_ABCD);
            //#region NJ PLC测试
            ////IDevices devices = EquipmentFactory.NewSocketDevice(230, "192.168.10.100", 9600, XingLucifer.IBase.Enums.DeviceTypes.CIP_Omron, 1, XingLucifer.IBase.Enums.TaskTypes.扫描信号, "测试", new System.Threading.CancellationTokenSource(), null);
            ////if (!devices.Open())
            ////{
            ////    Console.WriteLine("PLC通信失败!!!");
            ////    Console.ReadLine();
            ////    return;
            ////}
            ////Console.WriteLine("PLC通信成功");
            ////Console.WriteLine("写入ID");
            ////int stucrt_address = 2000;

            ////for (int i = 0; i < 4; i++)
            ////{
            ////    devices.WriteSingle("123456", stucrt_address + (27 * i), 0, 34);
            ////    BatteryModel model = new BatteryModel();
            ////    model.ID = 123456;
            ////    model.Results_PC = 6;
            ////    devices.WriteClass(model, 2017 + (27 * i));
            ////}

            ////Console.WriteLine("读取ID");
            ////for (int i = 0; i < 4; i++)
            ////{
            ////    BatteryArrayModel batteryArray = new BatteryArrayModel(1);
            ////    var model = devices.ReadClass(2017 + (i * 27), batteryArray);
            ////    Console.WriteLine($"条码：{devices.ReadSingle<string>(2000 + (i * 27), 34)}");
            ////    Console.WriteLine($"ID：{batteryArray.Batterys[0].ID}");
            ////}
            //#endregion
            //#region 注液泵 modbus tcp测试
            ////IDevices devices = EquipmentFactory.NewSocketDevice(220, "192.168.250.1", 9600, XingLucifer.IBase.Enums.DeviceTypes.NX_FINS_PLC, 1, XingLucifer.IBase.Enums.TaskTypes.扫描信号, "测试", cancellationTokenSource, com);
            ////if (!devices.Open())
            ////{
            ////    Console.WriteLine("设备通信失败!!!");
            ////    Console.ReadLine();
            ////    return;
            ////}
            ////Console.WriteLine("设备通信成功,开始写入注液量");
            ////if (devices.WriteSingle(6395, 3500, 0, 0))
            ////{
            ////    Console.WriteLine("写入注液量成功");
            ////}
            ////else
            ////{
            ////    Console.WriteLine("写入注液量失败");
            ////}
            ////Console.WriteLine("读取注液量");
            ////ushort value = devices.ReadSingle<ushort>(3500, 0);
            ////Console.WriteLine($"读取到的注液量为：{value}");
            //#endregion
            //#region modbus rtu测试
            ////IDevices devices = EquipmentFactory.NewSocketDevice(200, "COM2", 9600, XingLucifer.IBase.Enums.DeviceTypes.DTSU666, 1, XingLucifer.IBase.Enums.TaskTypes.DTSU666, "测试", cancellationTokenSource, com);
            ////if (!devices.Open())
            ////{
            ////    Console.WriteLine("设备通信失败!!!");
            ////    Console.ReadLine();
            ////    return;
            ////}
            ////Console.WriteLine("设备通信成功,开始写入数值");
            ////if (devices.WriteSingle((short)666, 0, 0, 0))
            ////{
            ////    Console.WriteLine("写入注液量成功");
            ////}
            ////else
            ////{
            ////    Console.WriteLine("写入注液量失败");
            ////}
            ////Console.WriteLine("读取浮点数");
            ////float value = devices.ReadSingle<float>(12, 0);
            ////Console.WriteLine($"读取到的浮点数为：{value}");
            //#endregion
            //cancellationTokenSource.Cancel();
            //devices.Close();
            //Console.WriteLine("关闭通信");
            //Console.ReadLine();
        //}

    }
}
